package com.syygl.test.study.leetCode.newStart;

public class Calculate {

    public Node calculateByLinked(Node head1, Node head2){
        if(head1 == null && head2 == null) return null;
        if(head1 == null) return head2;
        if(head2 == null) return head1;

        int tmp = 0;
        Node resultHead = new Node();
        Node index = resultHead;
        while (head1 != null || head2 != null){
            Node resultNode = new Node();
            resultNode.val = (head1.val + head2.val + tmp)%10;
            tmp = (head1.val + head2.val + tmp)/10;

            head1 = head1.next;
            head2 = head2.next;

            index.next = resultNode;
            index = resultNode;
        }
        if(tmp != 0){
            Node tail = new Node();
            tail.val = tmp;

            index.next = tail;
        }

        return resultHead.next;

    }

}


class Node {
    public int val;
    public Node next;
}